package com.asp.alumnisystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.asp.alumnisystem.adapter.DetailsAdapter;
import com.asp.alumnisystem.app.App;
import com.asp.alumnisystem.base.BaseActivity;
import com.asp.alumnisystem.base.BaseBean;
import com.asp.alumnisystem.base.BasePresenter;
import com.asp.alumnisystem.base.IView;
import com.asp.alumnisystem.model.DetailsModel;
import com.asp.alumnisystem.model.ForumModel;
import com.asp.alumnisystem.model.PageModel;
import com.asp.alumnisystem.presenter.DetailsPresenter;
import com.asp.alumnisystem.utils.ToastUtil;
import com.asp.alumnisystem.view.CustomRecyclerView;
import com.asp.alumnisystem.view.TitleBar;
import com.asp.alumnisystem.view.TitleBar.OnTitleBarClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 论坛帖子详情界面
 */
public class DetailsActivity extends BaseActivity implements
        CustomRecyclerView.RecyclerListener, IView<List<Object>>, OnTitleBarClickListener {

    private CustomRecyclerView recyclerView;
    private ForumModel forumModel;
    private DetailsAdapter adapter;
    private DetailsPresenter presenter;
    private PageModel pageModel = new PageModel();

    @Override
    protected View getLayoutView() {
        recyclerView = new CustomRecyclerView(this);
        return recyclerView;
    }

    @Override
    protected int getLayoutId() {
        return 0;
    }

    @Override
    protected void initView() {
        forumModel = (ForumModel) getIntent().getSerializableExtra("model");
        TitleBar titleBar = recyclerView.getTitleBar();
        titleBar.setTitleStr("帖子详情");
        adapter = new DetailsAdapter();
        recyclerView.setAdapter(adapter);
        recyclerView.setListener(this);
        presenter = new DetailsPresenter(this, forumModel.getForumId());
        managerPresenter(presenter);
        onRefresh(null);

        if (!App.getApp().isVisitor()){
            titleBar.setOptStr("回复");
            titleBar.setListener(this);
        }
    }

    @Override
    public void onBack() {
        finish();
    }

    @Override
    public void onRefresh(RecyclerView recyclerView) {
        pageModel.reset();
        presenter.work(pageModel);
    }

    @Override
    public void loadMore(RecyclerView recyclerView) {
        if (pageModel.isHaveNextAndToast(this)){
            presenter.work(pageModel);
        }else{
            loadDone(presenter);
        }
    }

    @Override
    public void loadSuccess(BasePresenter presenter, BaseBean<List<Object>> result) {
        if (result.success){
            if (pageModel.isFirstPage()){
                List<Object> list = new ArrayList<>();
                list.add(forumModel);
                list.addAll(result.data);
                adapter.setList(list);
            }else{
                adapter.addList(result.data);
            }
            pageModel.setRequestCount(result.data.size());
        }else{
            loadFail(presenter, result.error);
        }
    }

    @Override
    public void loadFail(BasePresenter presenter, String error) {
        super.loadFail(presenter, error);
        ToastUtil.showShortToast(error);
    }

    @Override
    public void loadDone(BasePresenter presenter) {
        recyclerView.stopRefreshing();
    }

    @Override
    public boolean onClickBackView() {
        return false;
    }

    @Override
    public void onClickOptView() {
        Intent intent = new Intent(this, ReplyActivity.class);
        intent.putExtra("id", forumModel.getForumId());
        startActivityForResult(intent, 10000);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 10000 && resultCode == Activity.RESULT_OK && data != null){
            List<Object> list = adapter.getList();
            if (list == null){
                list = new ArrayList<>();
            }
            DetailsModel model = (DetailsModel) data.getSerializableExtra("model");
            model.setAuthorInfo(App.getApp().getUserModel());
            if (list.size() > 1){
                list.add(1, model);
            }else{
                list.add(model);
            }
            adapter.setList(list);
        }
    }
}
